package com.androidhuman.example.MyBookmark;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class MyBookmark extends ListActivity {
    private SimpleCursorAdapter adapter;
    private BookmarkDB bookmarkDB;
    private Cursor cursor;
    
    private static final int MENU_ADD = 0;
    private static final int MENU_EDIT = 1;
    private static final int MENU_DELETE =2;
        
    private void init(){
    	cursor = bookmarkDB.getBookmarks();
    	startManagingCursor(cursor);
    	String[] from = new String[]{Bookmark.sites.NAME, Bookmark.sites.ADDRESS};
    	int[] to = new int[]{R.id.row_name, R.id.row_address};
    	
    	adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
    	setListAdapter(adapter);
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bookmarkDB = new BookmarkDB(this);
        registerForContextMenu(getListView());
    }
        
    @Override
    public void onResume(){
    	super.onResume();
    	bookmarkDB.open();
    	init();
    }
    
    @Override
    public void onPause(){
    	super.onPause();
    	bookmarkDB.close();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
    	super.onCreateOptionsMenu(menu);
    	menu.add(0, MENU_ADD, Menu.NONE, "북마크 추가")
    		.setIcon(android.R.drawable.ic_menu_add);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	super.onOptionsItemSelected(item);
    	switch(item.getItemId()){
    	case MENU_ADD:
    		LayoutInflater li = getLayoutInflater();
    		final View v = li.inflate(R.layout.edit, null);
    		new AlertDialog.Builder(this)
    			.setTitle("북마크 편집")
    			.setView(v)
    			.setPositiveButton("확인", new DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {
						EditText name = (EditText)v.findViewById(R.id.edit_name);
						EditText address = (EditText)v.findViewById(R.id.edit_address);
						bookmarkDB.insertBookmark(name.getText().toString(), 
								address.getText().toString());
						cursor.requery();
					}
    				
    			})
    			.setNegativeButton("취소", new DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// Do Nothing						
					}
    				
    			}).show();
    		return true;
    	}
    	return false;
    }
    
    @Override
    public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo){
    	super.onCreateContextMenu(menu, v, menuInfo);
    	menu.add(0, MENU_EDIT, Menu.NONE, "수정");
    	menu.add(0, MENU_DELETE, Menu.NONE, "삭제");
    }
    
    @Override
    public boolean onContextItemSelected (MenuItem item){
    	super.onContextItemSelected(item);
    	final AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
    	
    	switch(item.getItemId()){
    	case MENU_EDIT:
    		LayoutInflater li = getLayoutInflater();
    		final View v = li.inflate(R.layout.edit, null);
    		final EditText name = (EditText)v.findViewById(R.id.edit_name);
			final EditText address = (EditText)v.findViewById(R.id.edit_address);
			
			Cursor c = bookmarkDB.getBookmark(info.id);
			String nameStr = c.getString(c.getColumnIndex(Bookmark.sites.NAME));
			String addrStr = c.getString(c.getColumnIndex(Bookmark.sites.ADDRESS));
			
			name.setText(nameStr);
			address.setText(addrStr);
			
    		new AlertDialog.Builder(this)
    			.setTitle("북마크 편집")
    			.setView(v)
    			.setPositiveButton("확인", new DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						bookmarkDB.updateBookmark(info.id, name.getText().toString(), 
								address.getText().toString());
						cursor.requery();
					}
    				
    			})
    			.setNegativeButton("취소", new DialogInterface.OnClickListener(){

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// Do Nothing						
					}
    				
    			}).show();
    		
    		return true;
    	case MENU_DELETE:
    		bookmarkDB.deleteBookmark(info.id);
    		cursor.requery();
    		return true;
    	}
    	return false;
    }
}